home *** CD-ROM | disk | FTP | other *** search
- /*
- *************************************************************************
- *
- * A Basic Window class
- *
- * A generic simple window that can either show up by itself
- * (and be dragged around on the screen and closed by clicking a
- * "go-away" button)
- * or appear as a special item in a dialog
- *
- * Class ScreenWindow provides a very generic event and control processing
- * that is common to regular windows with controls, modal and modeless
- * dialogs. The basic functionality is to route events to appropriate
- * handlers, process close/destroy/etc events, and handle controls.
- * The class ScreenWindow provides only basic event handling, and it's
- * abstract: you have to create your own class on the base of ScreenWindow
- * and define the draw() function that is called on Update event to
- * paint/repaint the window. You may want to redefine couple of other
- * vanilla handlers (say, for control items (buttons, etc)).
- *
- * When you create the window using explicit specification of the rectangle, title, etc,
- * the window would be invisible right after the creation. It's better to set up
- * invisible (or hidden) attribute in the window resource if you create
- * a window from the resource template. That way you still can do some
- * rearrangements (say, create child windows) before the event processing
- * starts.
- *
- * To show the window (with all the controls, children etc) and start
- * processing of events, call show(). If you need to wait until the window
- * is closed (after the user clicked some button or clicked at the go-away
- * region, or selected "Close" from the menu, or something) call the
- * serve_to_death() function. It returns only when all the opened windows
- * are closed.
- *
- * $Id: Window.h,v 2.2 1994/11/07 20:33:30 oleg Exp oleg $
- *
- ***********************************************************************
- */
-
- #ifndef __GNUC__
- #pragma once
- #else
- #pragma interface
- #endif
-
- #ifndef _Window_h
- #define _Window_h 1
-
- /*
- *----------------------------------------------------------------------
- * Service functions
- */
-
- class IMAGE; // Opaque class
-
- class ScreenRect : public Rect
- {
- public:
- ScreenRect(const Rect& rect) : Rect(rect) {}
- ScreenRect(const IMAGE& image);
- // ScreenRect(const rowcol& heightwidth);
- // Create a rectangle of given height/width
- // positioned at a given point
- // ScreenRect(const rowcol& origin, const rowcol& heightwidth);
- ScreenRect& operator += (const int offset);
- operator Rect * (void) { return this; }
- int q_width(void) const { return right >= left ? right - left : left - right; }
- int q_height(void) const { return bottom >= top ? bottom - top : top - bottom; }
- // Give a STATIC string representation of
- // the rectangle
- // operator const char * (void) const;
- void print(const char * title = "") const;
- };
-
-
- // Make it easy setting and resetting
- // the current GrafPtr
- class SetNewGrafPtr
- {
- GrafPtr old_port;
- public:
- // Switch to an off-screen grafworld
- SetNewGrafPtr(const GrafPtr new_grafport) { GetPort(&old_port); SetPort(new_grafport); }
- // Restore the original grafworld
- ~SetNewGrafPtr(void) { SetPort(old_port); }
- };
-
- /*
- *----------------------------------------------------------------------
- * A generic simple window that can be dragged around the screen
- * and closed by clicking a "go-away" button
- * No other events are handled, redefine the event handler if necessary.
- */
-
- class ScreenWindow
- {
- friend class ModelessDialog;
-
- WindowPtr this_window;
- // Boolean Not_canceled; // set to FALSE if Cancel button was pressed
- // or the window was forcibly closed
-
- // static long universal_handler(WINDOW win, EVENT *ep);
- // static int no_opened_windows;
-
- virtual Boolean handle_mouse_down(const EventRecord& the_event, WindowPtr where_window, short window_part);
- virtual Boolean handle_key_down(const EventRecord& the_event); // Handles key_down & auto_key events
- void update(void);
- virtual void draw(void) = 0; // It is this function that really draws smth
-
- ScreenWindow(void) : this_window(nil) {} // Constructor that does nothing, and initializes
- // nothing. Trick to emulate a virtual constructor
- protected:
- WindowPtr our_window(void) const { return this_window; }
-
- // Draw w/o waiting for an UPDATE event:
- // Use for animation
- void draw_immediately(void) { draw(); /*?update()?? */}
-
- virtual void destroy_it(void); // I wouldn't've needed this is destructor had been
- // virtual. Right now, CW 6.0 accepts attribute virtual
- // for the destructor, but doesn't override
- // destructors as it does for virtual functions.
- // Oh, well, another kludge
-
- public:
- ScreenWindow(ScreenRect rect, const char * title);
- ScreenWindow(const short resource_id); // Create a window from a resource template
- ~ScreenWindow(void) { destroy_it(); }
- void refresh(void);
- // Serve opened windows until all get closed
- // static void serve_to_death(void);
-
- // Call this to start the service
- void show(const Boolean onoff=TRUE) const { ShowHide(this_window, onoff); }
- // virtual void cancel(void); // Close the window
- // operator Boolean (void) const { return Not_canceled; }
- //Boolean is_this_window(const WindowPtr some_window) const { return some_window == this_window; }
- //Boolean operator == (const WindowPtr some_window) const { return is_this_window(some_window); }
-
- // ScreenWindow event dispatch entry points
- // Return FALSE if this object doesn't want any
- // more events
- virtual Boolean handle_null_event(const long event_time);
- virtual Boolean handle_event(const EventRecord& the_event);
- };
-
- /*
- *----------------------------------------------------------------------
- * An off-screen pixel buffer for faster drawing
- */
-
- class OffScreenBuffer
- {
- GWorldPtr graf_world; // (Offscreen) graphical world that contains the picture
-
- protected:
- PixMapHandle pixmap; // Pixmap for the offscreen world
- int _height; // Dimensions of the pixmap (precomputed for
- int _width; // easy reference
- int _bytes_per_row; // Note, bytes_per_row >= width (and generally, not equal)
-
- // Draw the offscreen buffer on screen
- void draw(const Rect& where_rect, const Rect& from_rect); // only part of it
- void draw(const Rect& where_rect) { draw(where_rect,(**pixmap).bounds); } // or the whole
-
- GrafPtr set_this_grafptr(void) const // Set this grafptr and return the old one
- { GrafPtr old_port; GetPort(&old_port); SetPort((GrafPtr)graf_world); return old_port; }
- public:
- OffScreenBuffer(ScreenRect rect, const short clut_id=0);
- ~OffScreenBuffer(void);
-
- PixMapHandle get_pixmap(void) const { return pixmap; }
-
- // Bounding rect of the grafworld
- ScreenRect q_bounds(void) const { return (**pixmap).bounds; }
- int height(void) const { return _height; }
- int width(void) const { return _width; }
- int bytes_per_row(void) const { return _bytes_per_row; }
- };
-
- // Make it easy setting and resetting
- // the current window
- class Set_NewGrafWorld
- {
- CGrafPtr old_port;
- GDHandle old_gdevice;
- public:
- // Switch to an off-screen grafworld
- Set_NewGrafWorld(GWorldPtr graf_world)
- { GetGWorld(&old_port,&old_gdevice); SetGWorld((CGrafPtr)graf_world,nil); }
- // Restore the original grafworld
- ~Set_NewGrafWorld(void) { SetGWorld(old_port,old_gdevice); }
- };
-
- #endif
-